-
Notifications
You must be signed in to change notification settings - Fork 133
/
MenuRow.swift
61 lines (54 loc) · 1.5 KB
/
MenuRow.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import SwiftUI
struct MenuRow: View {
private let label: String
private let icon: String
@Binding private var count: Int
init(label: String, icon: String, count: Int = 0) {
self.label = label
self.icon = icon
_count = Binding.constant(count)
}
init(label: String, icon: String, count: Binding<Int>) {
self.label = label
self.icon = icon
_count = count
}
var countText: String {
guard count > 0 else {
return "0"
}
let ammendedCount = count > 99 ? "99+" : "\(count)"
return ammendedCount
}
var accessibilityLabel: String {
if count > 0 {
return "\(label), \(count)"
} else {
return label
}
}
var body: some View {
Label {
HStack {
Text(label)
Spacer()
Group {
Text(countText)
.font(.footnote.bold())
.padding(2)
.frame(minWidth: 20)
.background(.white)
.clipShape(RoundedRectangle(cornerSize: CGSize(width: 9, height: 9)))
.foregroundColor(.black)
}
.opacity(count > 0 ? 1 : 0)
}
.accessibilityLabel(accessibilityLabel)
} icon: {
Image(icon)
}
}
}
#Preview {
MenuRow(label: "Podcasts", icon: "podcasts", count: 10)
}